//
//  main.m
//  OC07_MRC省市区
//
//  Created by Hanlingyu on 16/7/12.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Zone.h"
#import "City.h"
#import "Province.h"
#import "TempCity.h"

int main(int argc, const char * argv[]) {
  
#pragma mark - 第四题
#if 0
    NSString *sourceStr = [NSString stringWithContentsOfFile:@"/Users/ltq/DLS160710/Objective-C/HomeWork/OC07_MRC省市区/OC07_MRC省市区/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [sourceStr componentsSeparatedByString:@"\n"];
    
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if (![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    
    NSLog(@"%@", allProArray);
#endif
    
    
#pragma mark - 第五题
#if 1
    NSString *locationPath = @"/Users/ltq/DLS160710/Objective-C/HomeWork/OC07_MRC省市区/OC07_MRC省市区/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
    // 根据",", 分割字符串, 得到所有城市的数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];

    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        // 根据"|"切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithCityName:[city firstObject] cityNumber:[city lastObject]];
        [allTempCityArray addObject:tempCity];
        [tempCity release];
    }
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    [sortTempCity release];
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:tempCity];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:tempCity];
            [pro.array addObject:city];
        } else {
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", finalClassArray);
    
    
#endif
    return 0;
}
